package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.pojo.GpmProjectHistoryEntity;
import com.topscomm.gpm.pojo.GpmWfFormEntity;
import com.topscomm.gpm.service.impl.GpmProjectHistoryService;
import com.topscomm.gpm.service.impl.GpmProjectTeamService;
import com.topscomm.gpm.service.impl.GpmWfFormService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description TODO
 * @Classname SdProjectHistoryScript
 * @Date 2021/12/13
 * @Created by wxh
 */
public class GpmProjectHistoryScript extends GpmScript {

	@Transactional(rollbackFor = Exception.class)
	public void saveBaseChangeLog(ITapService service, TapEntity entity) throws Exception {
		CboApprovalNodeInsEntity cboApprovalNodeInsEntity = (CboApprovalNodeInsEntity) entity;
		String id = ConvertUtil.convertToString(cboApprovalNodeInsEntity.getSourceid());
		GpmWfFormService gpmWfFormService = (GpmWfFormService) SpringUtil.getBean("gpmWfFormService");
		GpmWfFormEntity wfFormEntity = gpmWfFormService.queryById(id);
		if (wfFormEntity != null) {
			String sourceid = ConvertUtil.convertToString(wfFormEntity.getSourceid());
			gpmWfFormService.changeEditNodePass(sourceid);
		}
	}

	@Transactional
	public void projectChangeUpdate(ITapService service, TapEntity entity) throws Exception{
		CboApprovalNodeInsEntity cboApprovalNodeInsEntity = (CboApprovalNodeInsEntity) entity;
		String id = ConvertUtil.convertToString(cboApprovalNodeInsEntity.getSourceid());
		GpmWfFormService gpmWfFormService = (GpmWfFormService) SpringUtil.getBean("gpmWfFormService");
		GpmWfFormEntity wfFormEntity = gpmWfFormService.queryById(id);
		String sourceid = ConvertUtil.convertToString(wfFormEntity.getSourceid());
		GpmProjectHistoryService gpmProjectHistoryService = (GpmProjectHistoryService)SpringUtil.getBean("gpmProjectHistoryService");

		GpmProjectHistoryEntity gpmProjectHistoryEntity = gpmProjectHistoryService.queryById(sourceid);
		String projectcode = gpmProjectHistoryEntity.getProjectcode();
		gpmProjectHistoryService.projectChangeUpdate(projectcode, gpmProjectHistoryEntity);
	}

	/**
	 * @author
	 * @date
	 * @description: 新增立项结束，给项目组成员发送消息
	 * @modify:
	 */
	@Transactional
	public void projectChangeSendMessage(ITapService service, TapEntity entity) throws Exception {
		// 根据审批点实例获取项目编号
		CboApprovalNodeInsEntity cboApprovalNodeInsEntity = (CboApprovalNodeInsEntity) entity;
		String id = ConvertUtil.convertToString(cboApprovalNodeInsEntity.getSourceid());
		GpmWfFormService gpmWfFormService = (GpmWfFormService) SpringUtil.getBean("gpmWfFormService");
		GpmWfFormEntity wfFormEntity = gpmWfFormService.queryById(id);
		String sourceid = ConvertUtil.convertToString(wfFormEntity.getSourceid());
		GpmProjectHistoryService gpmProjectHistoryService = (GpmProjectHistoryService)SpringUtil.getBean("gpmProjectHistoryService");
		GpmProjectHistoryEntity gpmProjectHistoryEntity = gpmProjectHistoryService.queryById(sourceid);
		String sourcetype = GpmProjectHistoryEntity.tableName;
		GpmProjectTeamService gpmProjectTeamService = (GpmProjectTeamService) SpringUtil.getBean("gpmProjectTeamService");
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectcode = '{0}'", gpmProjectHistoryEntity.getProjectcode());
		// 消息内容
		String contentInfo = "立项";
		// 给项目组成员发送消息
		gpmProjectTeamService.sendMessageToGroupMember(sql.toString(), sourceid, sourcetype, contentInfo);
	}
}
